﻿using System;
using System.Web.UI;
using RCSP.Biz.Models;
using RCSP.Biz.Views;
using RCSP.Common;
using RCSP.Common.Enums;
using RCSP.DataAccess;

namespace RCSP.Biz.Presenters
{
    public class MemberPresenter : BasePresenter<IMemberView>
    {
        #region Variable:

        private IMemberRepository memberRep;

        public IMemberRepository MemberRep
        {
            get
            {
                if (memberRep == null)
                {
                    memberRep = new MemberRepository();
                }
                return memberRep;
            }
            set { memberRep = value; }
        }

        #endregion

        #region Contructor:

        public MemberPresenter(IMemberView view)
            : base(view)
        {
            memberRep = new MemberRepository();
        }

        #endregion

        #region Method:

        public override void DoAction()
        {
            string memberId = View.AccountNumber != null ? View.AccountNumber : string.Empty;
            switch (View.Mode)
            {
                case ControlMode.View:

                    if (!string.IsNullOrEmpty(memberId))
                    {
                        ShowMemberInfo(memberId);
                    }
                    break;
                case ControlMode.Create:
                    if (isExistedMember(View.AccountNumber))
                    {
                        AddNewMember(RequestType.Edit);
                    }
                    else
                    {
                        AddNewMember(RequestType.Add);
                    }
                    //int result = AddNewMember();

                    break;
                default:
                    break;
            }
        }

        private int AddNewMember(RequestType type)
        {
            return MemberRep.SaveMember(type, View.AccountNumber, View.KanaName, View.Name, View.GenderClassification, View.DateOfBirth, View.Zip, View.Address1, View.Address2, View.PhoneNumber, View.DateOfBirthYear, View.DateOfBirthMonth, View.DateOfBirthDay, View.AreaCode, View.BillingCode, View.BillingClassification,
                View.PaymentMethod, View.PaymentDivision, View.YearBillingMonth, 1, View.BankCode, View.BranchCode, View.DepositEvent, "アカウント", View.AccountName, View.ParentMemberShipNumber, View.WorkPostalCode, View.WorkAddress2, View.WorkPhoneNumber, View.MemberClassification, View.DMDestination, View.WithdrawalDate, View.ChangeProcessDate, View.ReasonForStoppingUse, View.Hospital,
                View.Contact, View.BillingCode, View.BillingDepartmentJobTitle, View.PhotoFileName, View.PhotoDate);
        }


        private void ShowMemberInfo(string memberId)
        {
            //int memberId = Convert.ToInt32(View.MemberNo);
            Member mem = MemberRep.GetMemberByID(memberId);
            if (mem != null)
            {
                View.AccountNumber = mem.AccountNumber;
                View.KanaName = mem.KanaName;
                View.GenderClassification = mem.GenderClassification;
                //View.DateOfBirthEraClassification = mem.DateOfBirthEraClassification;
                View.DateOfBirthYear = mem.DateOfBirthYear;
                View.DateOfBirthMonth = mem.DateOfBirthMonth;
                View.DateOfBirthDay = mem.DateOfBirthDay;
                View.DateOfBirth = mem.DateOfBirth;
                View.Zip = mem.Zip;
                View.Address1 = mem.Address1;
                View.PhoneNumber = mem.PhoneNumber;
                View.Address2 = mem.Address2;
                View.BillingDepartmentJobTitle = mem.BillingDepartmentJobTitle;
                View.EmployerResponsibleName = mem.EmployerResponsibleName;
                View.WorkPostalCode = mem.WorkPostalCode;
                View.WorkAddress2 = mem.WorkAddress2;
                View.WorkPhoneNumber = mem.WorkPhoneNumber;

                View.AreaCode = mem.AreaCode;

                View.MemberClassification = mem.MemberClassification;

                View.Hospital = mem.Hospital;
                View.Contact = mem.Contact;
                View.BillingClassification = mem.BillingClassification;
                View.PaymentMethod = mem.PaymentMethod;
                View.PaymentDivision = mem.PaymentDivision;
                View.YearBillingMonth = mem.YearBillingMonth;
                //View.NumberOfUnits =mem.NumberOfUnits;

                View.Name = mem.Name;
                View.PhotoDate = mem.PhotoDate;
                View.PhotoFileName = mem.PhotoFileName;

                View.ChangeProcessDate = mem.ChangeProcessDate;
                View.WithdrawalDate = mem.WithdrawalDate;
                View.ReasonForStoppingUse = mem.ReasonForStoppingUse;
                View.BillingCode = mem.BillingCode;
                View.DMDestination = mem.DMDestination;
                View.ParentMemberShipNumber = mem.ParentMemberShipNumber;
                View.BankCode = mem.BankCode;
                View.BranchCode = mem.BranchCode;
                View.DepositEvent = mem.DepositEvent;
                View.AccountName = mem.AccountName;
            }
        }

        private bool isExistedMember(string accountNumber)
        {
            Member mem = MemberRep.GetMemberByID(accountNumber);
            if (mem != null)
            {
                return true;
            }
            return false;
        }


        public bool BindAdressToTextBox()
        {
            PostalCode post = MemberRep.GetPostalCodeByZipCode(View.Zip);
            if (post != null)
            {
                if (post.State == null && post.City == null && post.TownArea == null)
                {
                    View.Address1 = string.Empty;
                    View.TownCollege = string.Empty;
                    return false;
                }
                else
                {
                    View.Address1 = post.State;
                    View.TownCollege = post.City + " " + post.TownArea;
                }
                return true;
            }
            else
            {
                View.Address1 = string.Empty;
                View.TownCollege = string.Empty;
            }
            return false;
        }

        public bool BindDistrictName() 
        {
            District dis = MemberRep.GetDistrictByStateCode(View.AreaCode);
            if (dis != null) 
            {
                if (dis.DistrictName == null)
                {
                    View.District = string.Empty;
                    return false;
                }
                else 
                {
                    View.District = dis.DistrictName;
                    return true;
                }
            }
            return false;
        }

        public bool BindBankName()
        {
            
            Bank bank = MemberRep.GetBankByBankCode(Convert.ToInt32(View.BankCode));
            if (bank != null)
            {
                if (bank.BankName == null)
                {
                    View.BankName = string.Empty;
                    return false;
                }
                else
                {
                    View.BankName = bank.BankName;
                    return true;
                }
            }
            return false;
        }

        public bool BindBranchName()
        {

            Branch branch = MemberRep.GetBranchByBranchCode(Convert.ToInt32(View.BranchCode));
            if (branch != null)
            {
                if (branch.BranchName == null)
                {
                    View.BranchName = string.Empty;
                    return false;
                }
                else
                {
                    View.BranchName = branch.BranchName;
                    return true;
                }
            }
            return false;
        }

        #endregion
    }
}